package com.alphaking.util;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * 系统上下文内容的相关操作类
 */
@Component
public class ApplicationContextUtil implements ApplicationContextAware {
    private static ApplicationContext context;
 
    public void setApplicationContext(ApplicationContext context) throws BeansException {
        ApplicationContextUtil.context = context;
    }
 
    public static ApplicationContext getContext() {
        return context;
    }
 
    public static <T> T getBean(String beanName) {
        return (T)context.getBean(beanName);
    }
    
    /**
     * 获取请求域名
     * @param request
     * @return
     */
    public static String getDomain(HttpServletRequest request){
    	StringBuffer url = request.getRequestURL();  
	    String tempContextUrl = url.delete(url.length() - request.getRequestURI().length(), url.length()).append("/").toString();
	    System.out.println("----"+tempContextUrl+"----");
	    String[] abc =  tempContextUrl.split("/");
	    System.out.println("----"+abc[2]+"----");
	    String[] def = abc[2].split(":");
	    String domain = def[0];
	    System.out.println("----"+domain+"----");
	    return tempContextUrl;
	    
	    /**
	     * 获取带部署环境上下文的域名，如： http://www.iteye.com/admin/ 
	     * StringBuffer url = request.getRequestURL();
	     * String tempContextUrl = url.delete(url.length() - request.getRequestURI().length(), url.length())
	     * .append(request.getServletContext().getContextPath())
	     * .append("/")
	     * .toString();
	     */
    }
 
}